package com.ruoyi.web.controller.business;

import java.util.List;

import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.business.service.IStatementItemService;
import com.ruoyi.business.service.IStatementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Appointment;
import com.ruoyi.business.service.IAppointmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 养修信息预约Controller
 * 
 * @author ruoyi
 * @date 2022-01-25
 */
@Controller
@RequestMapping("/business/appointment")
public class AppointmentController extends BaseController {
    private String prefix = "business/appointment";

    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IStatementService statementService;

    @RequiresPermissions("business:appointment:view")
    @GetMapping("/listPage")
    public String appointment() {
        return prefix + "/list";
    }

    /**
     * 查询养修信息预约列表
     */
    @RequiresPermissions("business:appointment:list")
    @PostMapping("/query")
    @ResponseBody
    public TableDataInfo query(Appointment appointment) {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return getDataTable(list);
    }

    /**
     * 导出养修信息预约列表
     */
    @RequiresPermissions("business:appointment:export")
    @Log(title = "养修信息预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Appointment appointment) {
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        ExcelUtil<Appointment> util = new ExcelUtil<Appointment>(Appointment.class);
        return util.exportExcel(list, "养修信息预约数据");
    }

    /**
     * 新增养修信息预约
     */
    @GetMapping("/addPage")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存养修信息预约
     */
    @RequiresPermissions("business:appointment:add")
    @Log(title = "养修信息预约", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Appointment appointment) {
        return toAjax(appointmentService.insertAppointment(appointment));
    }

    /**
     * 修改养修信息预约
     */
    @RequiresPermissions("business:appointment:edit")
    @GetMapping("/editPage/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Appointment appointment = appointmentService.selectAppointmentById(id);
        mmap.put("appointment", appointment);
        return prefix + "/edit";
    }

    /**
     * 修改保存养修信息预约
     */
    @RequiresPermissions("business:appointment:edit")
    @Log(title = "养修信息预约", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Appointment appointment) {
        return toAjax(appointmentService.updateAppointment(appointment));
    }

    /**
     * 删除养修信息预约
     */
    @RequiresPermissions("business:appointment:remove")
    @Log(title = "养修信息预约", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appointmentService.deleteAppointmentByIds(ids));
    }

    /**
     * 预约到店操作
     */
    @RequiresPermissions("business:appointment:gotoStatus")
    @Log(title = "预约到店操作", businessType = BusinessType.UPDATE)
    @PostMapping( "/gotoStatus/{id}")
    @ResponseBody
    public AjaxResult gotoStatus(@PathVariable("id") Long id) {
       appointmentService.updateStatusToShop(id);
        return AjaxResult.success();
    }
    /**
     * 取消预约操作
     */
    @RequiresPermissions("business:appointment:cancelStatus")
    @Log(title = "取消预约操作", businessType = BusinessType.UPDATE)
    @PostMapping( "/cancelStatus/{id}")
    @ResponseBody
    public AjaxResult cancelStatus(@PathVariable("id") Long id) {
       appointmentService.updateStatusToCancel(id);
        return AjaxResult.success();
    }
 /**
     * 结算单生成
     */
    @RequiresPermissions("business:appointment:generateStatement")
    @Log(title = "结算单生成", businessType = BusinessType.INSERT)
    @PostMapping( "/generateStatement")
    @ResponseBody
    public AjaxResult generateStatement(Long id) {
        return AjaxResult.success(statementService.generateStatement(id));
    }


}
